﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class AppLinkCateController : Base
{
    private readonly AppLinkCateService _appLinkCateService;
    public AppLinkCateController(AppLinkCateService appLinkCateService)
    {
        _appLinkCateService = appLinkCateService;
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.AppLink_Index)]
    public async Task<IActionResult> ListAsync()
    {
        List<AppLinkCate> cateList = await _appLinkCateService.GetAllListAsync("Id asc");
        return Success(new { data = cateList });
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.AppLink_Index)]
    public async Task<IActionResult> IndexAsync([FromQuery] QueryAppLinkCate query)
    {
        PagedInfo<AppLinkCate> list = await _appLinkCateService.GetPagesAsync(query.PageNum, query.PageSize);
        return Success(list);
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.AppLink_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        AppLinkCate data = await _appLinkCateService.GetIdAsync(id);
        return Success(new { data });
    }
    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.AppLink_AddOrEdit)]
    [Log(Name = "新增链接类别")]
    public async Task<IActionResult> CreatePostAsync([FromBody] AppLinkCate edit)
    {
        if (await _appLinkCateService.InsertAndDelCacheAsync(edit) !=null)
            return Success("新增链接类别: " + edit.CateName);
        else
            return Fail("新增失败");
    }
    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.AppLink_AddOrEdit)]
    [Log(Name = "修改链接类别")]
    public async Task<IActionResult> EditPostAsync([FromBody] AppLinkCate edit)
    {
        AppLinkCate AppLinkCate = await _appLinkCateService.GetIdAsync(edit.Id);
        if (AppLinkCate == null)
            return Fail("修改内容不存在");
        if (await _appLinkCateService.UpdateAndDelCacheAsync(edit) > 0)
            return Success("成功修改链接类别 " + edit.CateName);
        else
            return Fail("修改失败！");
    }
    [HttpDelete]
    [ActionPermissionFilter(Permission = PermissionKeys.AppLink_Delete)]
    [Log(Name = "删除链接类别")]
    public async Task<IActionResult> Delete(int id)
    {
        if (await _appLinkCateService.Delete(id))
            return Success("删除链接类别 ID：" + id);
        else
            return Fail("删除失败,可能该类别下有链接！");
    }
}
